import moment from 'moment'
import QRCode from 'qrcode'
import store from '../store'

// 油品消费小票打印模板
export function getFuelReceiptText (data) {
  const oilFullName = fuelNameConvert(data.fuelName)
  if (data.invoiceType === 1) {
    data.invoiceType = '消费开票'
  } else {
    data.invoiceType = '充值开票'
  }
  var settlementUnitPrice = data.fuelPrice - ((parseFloat(data.systemDiscount) + parseFloat(data.couponDiscount)) / data.fuelLitre)
  settlementUnitPrice = settlementUnitPrice.toFixed(2)
  var receiptText = `
<div>${data.stationName}</div>
<div>小票类型：加油消费</div>
<div>油枪号：${data.gunNo}</div>
<div>油品：${oilFullName}</div>
<div>挂牌价：${data.fuelPrice}元/升</div>
<div>结算单价：${settlementUnitPrice}元/升</div>
<div>加油升数：${data.fuelLitre}升</div>
<div>结算方式：${data.paymentTypeName}</div>
<div>===============================</div>
<div>应付金额：${data.fuelAmount}元</div>
<div>券优惠：${data.couponDiscount}元</div>
<div>系统优惠：${data.systemDiscount}元</div>
<div>实付金额：${data.payAmount}元</div>
`
// 加油卡显示油卡卡号和余额信息
  if (data.cardUid && data.cardNo) {
    receiptText += `
  <div>===============================</div>
  <div>加油卡号：${data.cardNo}</div>
  <div>公司名称：${data.cardName || ''}</div>
  <div>客户名称：${data.username || ''}</div>
  <div>车牌号码：${data.carNumber || ''}</div>
  <div>开票类型：${data.invoiceType}</div>
  <div>油卡余额：${data.balance}元</div>
    `
  }
  receiptText +=
        `
  <div>===============================</div>
  <div>操作员：${data.operator}</div>
  <div>班次：${data.shiftName}</div>
  <div>加油时间：${moment(data.fuelEndTime).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>结算时间：${moment(data.operationTime).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>打印时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>===============================</div>
  <div>谢谢光临，欢迎下次再来!</div>
  <div>如需发票，请在当日开具.</div>
    ` + getQRCodeImageElement(data) + `
  </br>
  </br>
  </br>
  </br>
  </br>
  <div>.</div>
  `
  return receiptText
}

// 油品退款小票打印模板
export function getFuelRefundReceiptText (data) {
  const oilFullName = fuelNameConvert(data.fuelName)
  var receiptText = `
  <div>${data.stationName}</div>
  <div>小票类型：加油退款</div>
  <div>油枪号：${data.gunNo}</div>
  <div>油品：${oilFullName}</div>
  <div>挂牌价：${data.fuelPrice}元/升</div>
  <div>加油升数：${data.fuelLitre}升</div>
  <div>结算方式：${data.paymentTypeName}</div>
  <div>===============================</div>
  <div>应付金额：${data.fuelAmount}元</div>
  <div>券优惠：${data.couponDiscount}元</div>
  <div>系统优惠：${data.systemDiscount}元</div>
  <div>实付金额：${data.payAmount}元</div>
  <div>退款金额：${data.payAmount}元</div>
  `
  // 加油卡显示油卡卡号和余额信息
  if (data.cardUid && data.cardNo) {
    receiptText += `
  <div>===============================</div>
      `
    receiptText += `
  <div>加油卡号：${data.cardNo}</div>
      `
    receiptText += `
  <div>油卡余额：${data.balance}元</div>
      `
  }
  receiptText +=
        `
  <div>===============================</div>
  <div>操作员：${data.operator}</div>
  <div>班次：${data.shiftName}</div>
  <div>加油时间：${moment(data.fuelEndTime).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>结算时间：${moment(data.operationTime).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>退款时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>打印时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>===============================</div>
  <div>谢谢光临，欢迎下次再来!</div>
  </br>
  </br>
  </br>
  </br>
  </br>
  <div>.</div>
  `
  return receiptText
}

// 非油消费小票打印模板
export function getCommodityReceiptText (data) {
  let tmpList = data.attach.split(';')
  let commodity = ''
  let attach = `
  <div>-------------------------------</div>
  <div>
  <span>名称</span>
  <span style='margin-left:22px;'>数量</span>
  <span style='margin-left:22px;'>单价</span>
  <span style='margin-left:22px;'>金额</span>
  `
  for (let i = 0; i < tmpList.length; i++) {
    commodity = tmpList[i].split(',')
    attach += `
    <div>${commodity[0]}</div>
    <div>
    <span style='margin-left:60px;'>${commodity[1]}</span>
    <span style='margin-left:36px;'>${commodity[2]}</span>
    <span style='margin-left:17px;'>${commodity[3]}</span>
    </div>
    `
  }
  if (data.discountDetails) {
    attach += `<div>促销活动：${data.discountDetails}</div>`
  }
  attach += `
  <div>-------------------------------</div>
  `
  var receiptText = `
  <div>${data.stationName}</div>
  <div>小票类型：非油消费</div>
  ` + attach +
        `
  <div>商品数量：${data.quantity}</div>
  <div>支付方式：${data.payTypeName}</div>
  <div>===============================</div>
  <div>应付金额：${data.amount}元</div>
  <div>券优惠：${data.couponDiscount}元</div>
  <div>系统优惠：${data.memberDiscount}元</div>
  <div>实付金额：${data.payAmount}元</div>
  <div>===============================</div>
  <div>操作员：${data.salesman}</div>
  <div>结算时间：${moment(data.payDate).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>打印时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>===============================</div>
  <div>谢谢光临，欢迎下次再来!</div>
  ` + getQRCodeImageElementWithMemberInfo(data) +
        `
  </br>
  </br>
  </br>
  </br>
  </br>
  <div>.</div>
  `
  return receiptText
}

// 非油退款小票打印模板
export function getCommodityRefundReceiptText (data) {
  let tmpList = data.attach.split(';')
  let attach = `<div>商品名称：</div>
  <div>-------------------------------</div>
  `
  for (let i = 0; i < tmpList.length; i++) {
    attach += `
    <div>${tmpList[i]}</div>
    `
  }
  if (data.discountDetails) {
    attach += `<div>促销活动：${data.discountDetails}</div>`
  }
  attach += `
  <div>-------------------------------</div>
  `
  var receiptText = `
  <div>${data.stationName}</div>
  <div>小票类型：非油退款</div>
  ` + attach +
        `
  <div>商品数量：${data.quantity}</div>
  <div>支付方式：${data.payTypeName}</div>
  <div>===============================</div>
  <div>应付金额：${data.amount}元</div>
  <div>券优惠：${data.couponDiscount}元</div>
  <div>系统优惠：${data.memberDiscount}元</div>
  <div>实付金额：${data.payAmount}元</div>
  <div>退款金额：${data.payAmount}元</div>
  <div>===============================</div>
  <div>操作员：${data.salesman}</div>
  <div>结算时间：${moment(data.payDate).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>退款时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>打印时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>===============================</div>
  <div>谢谢光临，欢迎下次再来!</div>
  </br>
  </br>
  </br>
  </br>
  </br>
  <div>.</div>
  `
  return receiptText
}

// 储值卡充值小票打印模板
export function getCardRechargeReceiptText (data) {
  var receiptText = `
  <div>${data.stationName}</div>
  <div>小票类型：油卡充值</div>
  <div>支付渠道：${data.paymentChannel}</div>
  <div>油卡卡号：${data.cardNo}</div>
  <div>支付方式：${data.paymentTypeName}</div>
  <div>充值金额：${data.amount}元</div>
  <div>充值后：${data.balance}元</div>
  <div>===============================</div>
  <div>充值时间：${data.rechargeDate}</div>
  <div>操作员：${data.operator}</div>
  <div>班次：${data.shiftName}</div>
  <div>打印时间：${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}</div>
  <div>===============================</div>
  <div>谢谢光临，欢迎下次再来!</div>
  <div>如需发票，请在当日开具.</div>
  `
  return receiptText
}

// TODO 储值卡充值退款小票打印模板
export function getCardRechargeRefundReceiptText (data) {
}

/**
 * 获取二维码显示内容
 * @param {*} data
 * @returns
 */
function getQRCodeImageElementWithMemberInfo (data) {
  if (!data.qrcode) {
    return ''
  }
  return `<img style="margin-left: 20px;;margin-top: 10px;max-width: 180px;max-height: 180px;" id="qrcode" src="${data.qrcode}"/>` +
        `<div>电话号码：${data.memberPhone}</div>` +
        `<div>会员昵称：${data.memberName}</div>`
}

function getQRCodeImageElement (data) {
  console.log('getQRCodeImageElement')
  if (!data.qrcode) {
    return ''
  }
  return `<img style="margin-left: 20px;;margin-top: 10px;max-width: 180px;max-height: 180px;" id="qrcode" src="${data.qrcode}"/>`
}

function fuelNameConvert (fuelName) {
  const dataList = store.getters.fuelList.filter(item => {
    return item.fuelName === fuelName
  })
  return dataList.length > 0 ? dataList[0].fullName : fuelName
}

/**
 * 获取消费记录的开票二维码图片地址
 * @param data
 * @returns {Promise<T | string>}
 */
export function getSaleFuelInvoiceQrcode (data) {
  return QRCode.toDataURL(store.getters.wxServerUrl + '/pages/invoice/apply.html?id=' + data.fuelRecordId)
    .then(url => {
      console.log(url)
      return url
    })
    .catch(err => {
      console.error(err)
      return ''
    })
}
